Param($SrcPath, $DstPattern)

$all_start = Get-Date

$global:n_episodes = 0
$global:n_samples = 0

Write-Output "Looking tv releases in '$SrcPath'..."

function CreateTvReleaseStructure([string] $WorkPath) {
    $sample_path = Join-Path $WorkPath "Sample"
    $items = Get-ChildItem $WorkPath | where {! $_.PSIsContainer}
    ForEach ($item in $items) {
#        if ($item -match "(\d{2}[a-z]?_|s\d{2}e\d{2}_|)(.*?)(_SUBMUX|_se|_fixed|_CD.|_\d|)\.(avi|mkv|mp4)$") {
        if ($item -match "(.*)\.(\d)(\d{2})\..*\.rar$") {
            $tvrel_pre = $matches[1]
            $tvrel_season = $matches[2]
            $tvrel_episode = $matches[3]
            $tvrel_se = $tvrel_season + $tvrel_episode
            $tvrel_sestring = ".S0" + $tvrel_season + "E" + $tvrel_episode
            $tvrel_src_name = $tvrel_pre + "." + $tvrel_se + ".*"
            $tvrel_dst_name = $DstPattern -replace ".%s", $tvrel_sestring
            $tvrel_src_fullname = Join-Path $WorkPath $tvrel_src_name
            $tvrel_dst_fullname = Join-Path $WorkPath $tvrel_dst_name
            if (!(Test-Path $tvrel_dst_fullname)) {
                Write-Host "Creating " -nonewline; Write-Host "$tvrel_dst_fullname" -f green -nonewline
                $global:n_episodes++
                New-Item "$tvrel_dst_fullname" -itemType directory | Out-Null
                Move-Item "$tvrel_src_fullname" "$tvrel_dst_fullname"
                # Try to find matching sample
                $samples = Get-ChildItem $sample_path | where {! $_.PSIsContainer}
                $sample_found = $false
                ForEach ($sample in $samples) {
                    if ($sample -match ("(.*\.)" + $tvrel_se + "\.")) {
                        $global:n_samples++
                        $sample_found = $true
                        $sample_src_fullname = Join-Path $sample_path ($matches[1] + $tvrel_se + ".*")
                        $sample_dst_fullname = Join-Path $tvrel_dst_fullname "Sample"
                        New-Item "$sample_dst_fullname" -itemType directory | Out-Null
                        Move-Item "$sample_src_fullname" "$sample_dst_fullname"
                    }
                }
                if ($sample_found) {
                    Write-Host " with Sample."
                }
                else {
                    Write-Host "."
                }
            }
            else {
                Write-Host "Destination path '$tvrel_dst_fullname' exist beforehand, skipping."
            }
        }
    }
    if (! (Test-Path (Join-Path $sample_path "*"))) {
        Write-Host "Removing Sample dir since it's now empty."
        Remove-Item $sample_path
    }
    else {
        Write-Host "Leaving Sample dir since it contains stuff."
    }
}

CreateTvReleaseStructure $SrcPath

function GetNow() {
    return Get-Date -format "yyyy-MM-dd HH:mm:ss"
}

$all_end = Get-Date
$total_time = New-Timespan $all_start $all_end
$now = GetNow
Write-Output "`n"
Write-Output "Finished at $now. Processing took $total_time."
Write-Output "Found $global:n_episodes episodes with $global:n_samples samples."
